<?php

namespace app\admin\controller\amount;

use app\api\controller\amount\Curl;
use app\common\controller\Backend;
use think\Cache;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Amount extends Backend
{

    /**
     * Amount模型对象
     * @var \app\admin\model\amount\Amount
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\amount\Amount;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    private $app_id = '1779971008866359';
    private $secret = '8864c1f31ce5e3d713b81f43fd4832d4d32785de';
    //刷新access_token POST
    public function refresh_token(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379); // 连接到 Redis 服务器，指定主机和端口
        $refresh_token = $redis->get('refresh_token'); // 从 Redis 获取值
        var_dump($refresh_token);
        $data = [
            'app_id' => $this->app_id,
            'secret' => $this->secret,
            'refresh_token' => $refresh_token,
        ];
        // 发起POST请求
        $curl = new Curl();
        $res = $curl->curlRequest([
            'url'=>'https://ad.oceanengine.com/open_api/oauth2/refresh_token/',
            'method'=>'POST',
            'headers'=>[
                "Content-Type:application/json;charset=UTF-8",
                "User-Agent:API Explorer"
            ],
        ],$data);
        $body = json_decode($res['body']);
        $redis->set('access_token', $body->data->access_token); // 存储键值对
        $redis->close();
    }
    //获取已授权账户
    public function advertisers(){
        $access_token = $this->request->param('access_token');
        $data = [
            'access_token' => $access_token,
        ];
        $curl = new Curl();
        $res = $curl->curlRequest([
            'url'=>'https://ad.oceanengine.com/open_api/oauth2/advertiser/get/',
            'method'=>'GET',
            'headers'=>[
                "Content-Type:application/json;charset=UTF-8",
                "User-Agent:API Explorer"
            ],
        ],$data);
        return json_decode($res['body']);
    }
    //获取广告主数据 GET
    public function advertisers_data(){
        $access_token = $this->request->param('access_token');
        $start_date = $this->request->param('start_date');
        $end_date = $this->request->param('end_date');
        $advertiser_ids = $this->request->param('advertiser_ids');
        $advertiserData = [];
        $advertiser_ids = json_decode($advertiser_ids);
        foreach ($advertiser_ids as $advertiser_id){
            $data = [
                'advertiser_id' => $advertiser_id,
                'start_date' => $start_date,
                'end_date' => $end_date,
                'fields'=>[
                    "cost","click","convert", "avg_show_cost", "convert_rate", "convert_cost", "avg_click_cost", "show", "ctr",
                    "deep_convert",'deep_convert_cost','deep_convert_rate',
                ]
            ];
            // 发起POST请求
            $curl = new Curl();
            $res = $curl->curlRequest([
                'url'=>'https://ad.oceanengine.com/open_api/2/report/advertiser/get/',
                'method'=>'GET',
                'headers'=>[
                    "Content-Type:application/json;charset=UTF-8",
                    "User-Agent:API Explorer",
                    "Access-Token:".$access_token
                ],
            ],$data);
            $resData = json_decode($res['body']);
            if($resData->code == 0) $advertiserData[] = $resData->data;
        }

        return $advertiserData;
    }

    public function advertisers_select(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379); // 连接到 Redis 服务器，指定主机和端口
        $access_token = $redis->get('access_token'); // 从 Redis 获取值
        $redis->close();
        $data = [
            'advertiser_id' => '1668381887237127'
        ];
        $curl = new Curl();
        $res = $curl->curlRequest([
            'url'=>'https://ad.oceanengine.com/open_api/2/majordomo/advertiser/select/',
            'method'=>'GET',
            'headers'=>[
                "Content-Type:application/json;charset=UTF-8",
                "User-Agent:API Explorer",
                "Access-Token:".$access_token
            ],
        ],$data);
        return json_decode($res['body']);
    }
}
